package com.cskaoyan.servlet;

import com.cskaoyan.bean.PageData1;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.service.RegionServlet;
import com.cskaoyan.service.Impls.RegionServletImpl;
import com.cskaoyan.util.JacksonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/region/*")
public class AdminRegionServlet extends CommonServlet {

    /**
     * GET http://localhost:8083/admin/region/list HTTP/1.1
     * <p>
     * 端口号是8083  http port 改为8083
     * 虚拟映射的时候 配置application context /
     * 最终提供的是ROOT.xml
     *
     * 相应报文的JSON字符:可以使用浏览器的开发人员工具查看
     * 这里这个data的格式是用来做分页，前端根据这些数据做分页
     * {
     *   "data":{
     *       "limit":31,
     *       "list":[],
     *       "page":1,
     *       "pages":1
     *       "total":31
     *   },
     *   "errmsg":"成功",
     *   "errno":0
     * }
     * 你要写对应的键值对信息 → 前端就是根据这些key来解析你的JSON字符串响应数据
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RegionServlet regionServlet = new RegionServletImpl();
        response.setContentType("application/json;charset=utf-8");
        PageData1 pageData = new PageData1();
        pageData.setErrmsg("成功");
        pageData.setErrno(0);
        pageData.setData(regionServlet.data());

        String jsonStr = JacksonUtil.write(pageData);
        response.getWriter().println(jsonStr);
    }


}
